**************************************************************************************************************************************************************************************************
**Code to reproduce results of "Gender Differences in Reactions to Failure in High-Stakes Competition: Evidence from the National College Entrance Exam Retakes", 2023, forthcoming at JPE Micro**
**************************************************************************************************************************************************************************************************

global path=""
cd "$path"

set more off
clear

use "Data\Ningxia_Retake_Analysis.dta",clear

local BaseControl male han hui urban yingjie age

rdbwselect retake x_eb, kernel(uniform)
global hselect_eb=e(h_mserd)

gen K_eb_15 = (1-abs(x_eb/15)) * (abs(x_eb)<=15)
gen K_eb_10 = (1-abs(x_eb/10)) * (abs(x_eb)<=10)
gen K_eb_20 = (1-abs(x_eb/20)) * (abs(x_eb)<=20)
gen K_eb_select = (1-abs(x_eb/$hselect_eb)) * (abs(x_eb)<=$hselect_eb)

***Figure 1***
preserve
collapse (mean) retake cutoffyiben cutofferben, by(year wenli score)

foreach i of numlist 2002 (1) 2009 {
	foreach j of numlist 1 2 {
		sum cutoffyiben if year==`i' & wenli==`j'
		local c_y_`i'_`j'=r(mean)
		sum cutofferben if year==`i' & wenli==`j'
		local c_e_`i'_`j'=r(mean)
}
}

line retake score if year==2009 & wenli==1 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2009_1', lcolor(cranberry)) xline(`c_y_2009_1', lcolor(green)) xlabel(`c_e_2009_1' "Tier 2" `c_y_2009_1' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2009, Art Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2009_1.gph",replace

line retake score if year==2009 & wenli==2 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2009_2', lcolor(cranberry)) xline(`c_y_2009_2', lcolor(green)) xlabel(`c_e_2009_2' "Tier 2" `c_y_2009_2' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2009, Science Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2009_2.gph",replace

gr combine "OutFiles\ScorePlot_2009_2.gph" "OutFiles\ScorePlot_2009_1.gph", cols(2) ysize(10) xsize(18) ycommon scheme(s1color)
graph export "OutFiles\Fig1.png",replace

restore

***Figure 2***
preserve
rddensity x_eb, c(0) h(15 15) kernel(uniform) level(95) plot plot_range(-45 45) hist_range(-45 45) hist_n(9 9) genvars(v1)
twoway (bar v1_hist_height v1_hist_center if v1_hist_group==0, barwidth(5) color(green) fintensity(25)) (bar v1_hist_height v1_hist_center if v1_hist_group==1, barwidth(5) color(dkorange) fintensity(25)) (line v1_f v1_grid if v1_group==0, lcolor(green)) (line v1_f v1_grid if v1_group==1, lcolor(dkorange)) (line v1_cil v1_grid if v1_group==0, lpattern(dash) lcolor(navy)) (line v1_cir v1_grid if v1_group==0, lpattern(dash) lcolor(navy)) (line v1_cil v1_grid if v1_group==1, lpattern(dash) lcolor(navy)) (line v1_cir v1_grid if v1_group==1, lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Density") yscale(range(0)) ylabel(0 0.001 0.002 0.003 0.004 0.005) legend(off) scheme(s1color)
graph export "OutFiles\Fig2.png",replace
restore

***Figure 3***
preserve
rdplot retake x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Probability of Retaking the NCEE") legend(off) scheme(s1color) 
graph export "OutFiles\Fig3.png",replace
restore

***Figure 4***
preserve
rdplot currentoutcome_std_score x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Standardized Score: Initial", size(large)) legend(off) scheme(s1color) 
graph save "OutFiles\RF_C_Score.gph",replace
restore

preserve
rdplot finaloutcome_std_score x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Standardized Score: Final", size(large)) legend(off) scheme(s1color) 
graph save "OutFiles\RF_F_Score.gph",replace
restore

preserve
rdplot currentoutcome_pct x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Ranking: Initial", size(large)) legend(off) scheme(s1color) 
graph save "OutFiles\RF_C_Pct.gph",replace
restore

preserve
rdplot finaloutcome_pct x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Ranking: Final", size(large)) legend(off) scheme(s1color) 
graph save "OutFiles\RF_F_Pct.gph",replace
restore

gr combine "OutFiles\RF_C_Score.gph" "OutFiles\RF_F_Score.gph", cols(2) ycommon scheme(s1color) 
graph save "OutFiles\RF_CF_Score.gph",replace

gr combine "OutFiles\RF_C_Pct.gph" "OutFiles\RF_F_Pct.gph", cols(2) ycommon scheme(s1color) 
graph save "OutFiles\RF_CF_Pct.gph",replace

gr combine "OutFiles\RF_CF_Score.gph" "OutFiles\RF_CF_Pct.gph", cols(1) ysize(10) xsize(18) scheme(s1color)
graph export "OutFiles\Fig4.png",replace

***Figure 5***
preserve
keep if male==1
rdplot retake x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Probability of Retaking the NCEE") title("Male")  legend(off) scheme(s1color) 
graph save "OutFiles\FS_Male.gph",replace
restore

preserve
keep if male==0
rdplot retake x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Probability of Retaking the NCEE")  title("Female") legend(off) scheme(s1color) 
graph save "OutFiles\FS_Female.gph",replace
restore

gr combine "OutFiles\FS_Male.gph" "OutFiles\FS_Female.gph", cols(2) ycommon ysize(10) xsize(18) scheme(s1color)
graph export "OutFiles\Fig5.png",replace

***Figures A1-A2***
preserve
collapse (mean) retake cutoffyiben cutofferben, by(year wenli score)

foreach i of numlist 2002 (1) 2009 {
	foreach j of numlist 1 2 {
		sum cutoffyiben if year==`i' & wenli==`j'
		local c_y_`i'_`j'=r(mean)
		sum cutofferben if year==`i' & wenli==`j'
		local c_e_`i'_`j'=r(mean)
}
}

line retake score if year==2002 & wenli==1 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2002_1', lcolor(cranberry)) xline(`c_y_2002_1', lcolor(green)) xlabel(`c_e_2002_1' "Tier 2" `c_y_2002_1' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2002, Art Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2002_1.gph",replace

line retake score if year==2002 & wenli==2 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2002_2', lcolor(cranberry)) xline(`c_y_2002_2', lcolor(green)) xlabel(`c_e_2002_2' "Tier 2" `c_y_2002_2' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2002, Science Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2002_2.gph",replace

gr combine "OutFiles\ScorePlot_2002_2.gph" "OutFiles\ScorePlot_2002_1.gph", cols(2) ysize(10) xsize(18) ycommon scheme(s1color)
graph save "OutFiles\ScorePlot_2002.gph",replace


line retake score if year==2003 & wenli==1 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2003_1', lcolor(cranberry)) xline(`c_y_2003_1', lcolor(green)) xlabel(`c_e_2003_1' "Tier 2" `c_y_2003_1' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2003, Art Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2003_1.gph",replace

line retake score if year==2003 & wenli==2 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2003_2', lcolor(cranberry)) xline(`c_y_2003_2', lcolor(green)) xlabel(`c_e_2003_2' "Tier 2" `c_y_2003_2' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2003, Science Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2003_2.gph",replace

gr combine "OutFiles\ScorePlot_2003_2.gph" "OutFiles\ScorePlot_2003_1.gph", cols(2) ysize(10) xsize(18) ycommon scheme(s1color)
graph save "OutFiles\ScorePlot_2003.gph",replace


line retake score if year==2004 & wenli==1 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2004_1', lcolor(cranberry)) xline(`c_y_2004_1', lcolor(green)) xlabel(`c_e_2004_1' "Tier 2" `c_y_2004_1' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2004, Art Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2004_1.gph",replace

line retake score if year==2004 & wenli==2 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2004_2', lcolor(cranberry)) xline(`c_y_2004_2', lcolor(green)) xlabel(`c_e_2004_2' "Tier 2" `c_y_2004_2' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2004, Science Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2004_2.gph",replace

gr combine "OutFiles\ScorePlot_2004_2.gph" "OutFiles\ScorePlot_2004_1.gph", cols(2) ysize(10) xsize(18) ycommon scheme(s1color)
graph save "OutFiles\ScorePlot_2004.gph",replace


line retake score if year==2005 & wenli==1 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2005_1', lcolor(cranberry)) xline(`c_y_2005_1', lcolor(green)) xlabel(`c_e_2005_1' "Tier 2" `c_y_2005_1' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2005, Art Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2005_1.gph",replace

line retake score if year==2005 & wenli==2 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2005_2', lcolor(cranberry)) xline(`c_y_2005_2', lcolor(green)) xlabel(`c_e_2005_2' "Tier 2" `c_y_2005_2' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2005, Science Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2005_2.gph",replace

gr combine "OutFiles\ScorePlot_2005_2.gph" "OutFiles\ScorePlot_2005_1.gph", cols(2) ysize(10) xsize(18) ycommon scheme(s1color)
graph save "OutFiles\ScorePlot_2005.gph",replace


line retake score if year==2006 & wenli==1 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2006_1', lcolor(cranberry)) xline(`c_y_2006_1', lcolor(green)) xlabel(`c_e_2006_1' "Tier 2" `c_y_2006_1' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2006, Art Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2006_1.gph",replace

line retake score if year==2006 & wenli==2 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2006_2', lcolor(cranberry)) xline(`c_y_2006_2', lcolor(green)) xlabel(`c_e_2006_2' "Tier 2" `c_y_2006_2' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2006, Science Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2006_2.gph",replace

gr combine "OutFiles\ScorePlot_2006_2.gph" "OutFiles\ScorePlot_2006_1.gph", cols(2) ysize(10) xsize(18) ycommon scheme(s1color)
graph save "OutFiles\ScorePlot_2006.gph",replace


line retake score if year==2007 & wenli==1 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2007_1', lcolor(cranberry)) xline(`c_y_2007_1', lcolor(green)) xlabel(`c_e_2007_1' "Tier 2" `c_y_2007_1' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2007, Art Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2007_1.gph",replace

line retake score if year==2007 & wenli==2 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2007_2', lcolor(cranberry)) xline(`c_y_2007_2', lcolor(green)) xlabel(`c_e_2007_2' "Tier 2" `c_y_2007_2' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2007, Science Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2007_2.gph",replace

gr combine "OutFiles\ScorePlot_2007_2.gph" "OutFiles\ScorePlot_2007_1.gph", cols(2) ysize(10) xsize(18) ycommon scheme(s1color)
graph save "OutFiles\ScorePlot_2007.gph",replace


line retake score if year==2008 & wenli==1 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2008_1', lcolor(cranberry)) xline(`c_y_2008_1', lcolor(green)) xlabel(`c_e_2008_1' "Tier 2" `c_y_2008_1' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2008, Art Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2008_1.gph",replace

line retake score if year==2008 & wenli==2 & score>=300 & score<=600, lcolor(ebblue) xaxis(1 2) xtitle("Total Score", axis(1)) xtitle("", axis(2)) xlabel(300 350 400 450 500 550 600, axis(1)) xline(`c_e_2008_2', lcolor(cranberry)) xline(`c_y_2008_2', lcolor(green)) xlabel(`c_e_2008_2' "Tier 2" `c_y_2008_2' "Tier 1", axis(2)) ytitle("Probability of Retaking the NCEE") title("Year 2008, Science Track") scheme(s1color)
graph save "OutFiles\ScorePlot_2008_2.gph",replace

gr combine "OutFiles\ScorePlot_2008_2.gph" "OutFiles\ScorePlot_2008_1.gph", cols(2) ysize(10) xsize(18) ycommon scheme(s1color)
graph save "OutFiles\ScorePlot_2008.gph",replace


gr combine "OutFiles\ScorePlot_2002.gph" "OutFiles\ScorePlot_2003.gph" "OutFiles\ScorePlot_2004.gph" "OutFiles\ScorePlot_2005.gph", cols(2) ysize(1) xsize(2.15) scheme(s1color)
graph export "OutFiles\FigA1.png",replace

gr combine "OutFiles\ScorePlot_2006.gph" "OutFiles\ScorePlot_2007.gph" "OutFiles\ScorePlot_2008.gph", cols(2) ysize(1) xsize(2.15) scheme(s1color)
graph export "OutFiles\FigA2.png",replace

restore

***Figure A3***
preserve
rdplot male x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Male") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Male.gph",replace
restore

preserve
rdplot han x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Han") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Han.gph",replace
restore

preserve
rdplot hui x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Hui") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Hui.gph",replace
restore

preserve
rdplot urban x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Urban") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Urban.gph",replace
restore

preserve
rdplot yingjie x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("First-Time Taker") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Yingjie.gph",replace
restore

preserve
rdplot age x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Age") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Age.gph",replace
restore

gr combine "OutFiles\Balancing_Male.gph" "OutFiles\Balancing_Han.gph" "OutFiles\Balancing_Hui.gph" "OutFiles\Balancing_Urban.gph" "OutFiles\Balancing_Yingjie.gph" "OutFiles\Balancing_Age.gph", cols(3) ysize(10) xsize(18) scheme(s1color)
graph export "OutFiles\FigA3.png",replace

***Figure A4***
preserve

local graphvar retake
local BaseControl male han hui urban yingjie age

qui {
reghdfe `graphvar' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta1=_b[below_eb]
gen se1=_se[below_eb]
reghdfe `graphvar' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta2=_b[below_eb]
gen se2=_se[below_eb]	
reghdfe `graphvar' below_eb x_eb int_below_eb `BaseControl' [w=K_eb_15] if abs(x_eb)<=15 , absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta3=_b[below_eb]
gen se3=_se[below_eb]
reghdfe `graphvar' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_15] if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta4=_b[below_eb]
gen se4=_se[below_eb]

reghdfe `graphvar' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta5=_b[below_eb]
gen se5=_se[below_eb]
reghdfe `graphvar' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl'  if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta6=_b[below_eb]
gen se6=_se[below_eb]	
reghdfe `graphvar' below_eb x_eb int_below_eb `BaseControl'  [w=K_eb_10] if abs(x_eb)<=10 , absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta7=_b[below_eb]
gen se7=_se[below_eb]
reghdfe `graphvar' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl'  [w=K_eb_10] if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta8=_b[below_eb]
gen se8=_se[below_eb]

reghdfe `graphvar' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta9=_b[below_eb]
gen se9=_se[below_eb]
reghdfe `graphvar' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta10=_b[below_eb]
gen se10=_se[below_eb]	
reghdfe `graphvar' below_eb x_eb int_below_eb `BaseControl' [w=K_eb_20] if abs(x_eb)<=20 , absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta11=_b[below_eb]
gen se11=_se[below_eb]
reghdfe `graphvar' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_20] if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta12=_b[below_eb]
gen se12=_se[below_eb]

reghdfe `graphvar' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta13=_b[below_eb]
gen se13=_se[below_eb]
reghdfe `graphvar' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta14=_b[below_eb]
gen se14=_se[below_eb]	
reghdfe `graphvar' below_eb x_eb int_below_eb `BaseControl' [w=K_eb_select] if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta15=_b[below_eb]
gen se15=_se[below_eb]
reghdfe `graphvar' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_select] if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid)
gen beta16=_b[below_eb]
gen se16=_se[below_eb]
}

gen id=_n
keep if id<=16
keep beta* se* id

gen beta=0
foreach i of numlist 1 (1) 16 {
	replace beta=beta`i' if id==`i'
	gen hib`i'=beta`i'+invnormal(0.975)*se`i'
	gen lob`i'=beta`i'-invnormal(0.975)*se`i'
	gen hibw`i'=beta`i'+invnormal(0.95)*se`i'
	gen lobw`i'=beta`i'-invnormal(0.95)*se`i'
}

gen hib=0
gen lob=0
gen hibw=0
gen lobw=0
foreach i of numlist 1 (1) 16 {
	replace hib=hib`i' if id==`i'
	replace lob=lob`i' if id==`i'
	replace hibw=hibw`i' if id==`i'
	replace lobw=lobw`i' if id==`i'
}

gen ido=0
replace ido=1 if id==1 | id==5 | id==9 | id==13 
replace ido=2 if id==2 | id==6 | id==10 | id==14 
replace ido=3 if id==3 | id==7 | id==11 | id==15 
replace ido=4 if id==4 | id==8 | id==12 | id==16


gen id2=id
replace id2=id+1 if id>=5 & id<=8
replace id2=id+2 if id>=9 & id<=12
replace id2=id+3 if id>=13 & id<=16
replace id=id2

twoway (scatter beta id if ido==1, color(green) msize(large) msymbol(O)) (scatter beta id if ido==2, color(green) msize(large) msymbol(S)) (scatter beta id if ido==3, color(ebblue) msize(large)  msymbol(Oh)) (scatter beta id if ido==4, color(ebblue) msize(large) msymbol(Sh)) (rcap hib lob id if ido==1, color(green)) (rcap hib lob id if ido==2, color(green)) (rcap hib lob id if ido==3, color(ebblue)) (rcap hib lob id if ido==4, color(ebblue)) (rscatter hibw lobw id if ido==1, color(green) msymbol(X)) (rscatter hibw lobw id if ido==2, color(green) msymbol(X)) (rscatter hibw lobw id if ido==3, color(ebblue) msymbol(X)) (rscatter hibw lobw id if ido==4, color(ebblue) msymbol(X)), yline(0) legend(order(1 "Linear, Uniform Kernel" 2 "Quadratic, Uniform Kernel" 3 "Linear, Triangular Kernel" 4 "Quadratic, Triangular Kernel") row(2)) xlabel(2.5 "Bandwidth: 15" 7.5 "Bandwidth: 10" 12.5 "Bandwidth: 20" 17.5 "Bandwidth: CCT") xscale(range(0 20)) xtitle("") ytitle("") yscale(range(0)) ylabel(0 0.02 0.04 0.06 0.08 0.1 0.12 0.14) scheme(s1color) ysize(10) xsize(18)
graph export "OutFiles\FigA4.png", replace
restore

***Figure A5***
preserve

local graphvar dif_std_score
local BaseControl male han hui urban yingjie age

qui {
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta1=_b[retake]
gen se1=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta2=_b[retake]
gen se2=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' [w=K_eb_15] if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta3=_b[retake]
gen se3=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_15] if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta4=_b[retake]
gen se4=_se[retake]

ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta5=_b[retake]
gen se5=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta6=_b[retake]
gen se6=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' [w=K_eb_10] if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta7=_b[retake]
gen se7=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_10] if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta8=_b[retake]
gen se8=_se[retake]

ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta9=_b[retake]
gen se9=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta10=_b[retake]
gen se10=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' [w=K_eb_20] if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta11=_b[retake]
gen se11=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_20] if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta12=_b[retake]
gen se12=_se[retake]

ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta13=_b[retake]
gen se13=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta14=_b[retake]
gen se14=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' [w=K_eb_select] if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta15=_b[retake]
gen se15=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_select] if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta16=_b[retake]
gen se16=_se[retake]
}

gen id=_n
keep if id<=16
keep beta* se* id

gen beta=0
foreach i of numlist 1 (1) 16 {
	replace beta=beta`i' if id==`i'
	gen hib`i'=beta`i'+invnormal(0.975)*se`i'
	gen lob`i'=beta`i'-invnormal(0.975)*se`i'
	gen hibw`i'=beta`i'+invnormal(0.95)*se`i'
	gen lobw`i'=beta`i'-invnormal(0.95)*se`i'
}

gen hib=0
gen lob=0
gen hibw=0
gen lobw=0
foreach i of numlist 1 (1) 16 {
	replace hib=hib`i' if id==`i'
	replace lob=lob`i' if id==`i'
	replace hibw=hibw`i' if id==`i'
	replace lobw=lobw`i' if id==`i'
}

gen ido=0
replace ido=1 if id==1 | id==5 | id==9 | id==13 
replace ido=2 if id==2 | id==6 | id==10 | id==14 
replace ido=3 if id==3 | id==7 | id==11 | id==15 
replace ido=4 if id==4 | id==8 | id==12 | id==16


gen id2=id
replace id2=id+1 if id>=5 & id<=8
replace id2=id+2 if id>=9 & id<=12
replace id2=id+3 if id>=13 & id<=16
replace id=id2

twoway (scatter beta id if ido==1, color(green) msize(large) msymbol(O)) (scatter beta id if ido==2, color(green) msize(large) msymbol(S)) (scatter beta id if ido==3, color(ebblue) msize(large)  msymbol(Oh)) (scatter beta id if ido==4, color(ebblue) msize(large) msymbol(Sh)) (rcap hib lob id if ido==1, color(green)) (rcap hib lob id if ido==2, color(green)) (rcap hib lob id if ido==3, color(ebblue)) (rcap hib lob id if ido==4, color(ebblue)) (rscatter hibw lobw id if ido==1, color(green) msymbol(X)) (rscatter hibw lobw id if ido==2, color(green) msymbol(X)) (rscatter hibw lobw id if ido==3, color(ebblue) msymbol(X)) (rscatter hibw lobw id if ido==4, color(ebblue) msymbol(X)), yline(0) legend(order(1 "Linear, Uniform Kernel" 2 "Quadratic, Uniform Kernel" 3 "Linear, Triangular Kernel" 4 "Quadratic, Triangular Kernel") row(2)) xlabel(2.5 "Bandwidth: 15" 7.5 "Bandwidth: 10" 12.5 "Bandwidth: 20" 17.5 "Bandwidth: CCT") xscale(range(0 20)) xtitle("") ytitle("Standardized Score") yscale(range(0)) ylabel(0 0.2 0.4 0.6 0.8) scheme(s1color) 
graph save "OutFiles\Robustness_2SLS_Score.gph", replace
restore


preserve

local graphvar dif_pct
local BaseControl male han hui urban yingjie age

qui {
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta1=_b[retake]
gen se1=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta2=_b[retake]
gen se2=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' [w=K_eb_15] if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta3=_b[retake]
gen se3=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_15] if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta4=_b[retake]
gen se4=_se[retake]

ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta5=_b[retake]
gen se5=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta6=_b[retake]
gen se6=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' [w=K_eb_10] if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta7=_b[retake]
gen se7=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_10] if abs(x_eb)<=10, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta8=_b[retake]
gen se8=_se[retake]

ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta9=_b[retake]
gen se9=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta10=_b[retake]
gen se10=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' [w=K_eb_20] if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta11=_b[retake]
gen se11=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_20] if abs(x_eb)<=20, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta12=_b[retake]
gen se12=_se[retake]

ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta13=_b[retake]
gen se13=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta14=_b[retake]
gen se14=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb `BaseControl' [w=K_eb_select] if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta15=_b[retake]
gen se15=_se[retake]
ivreghdfe `graphvar' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' [w=K_eb_select] if abs(x_eb)<=$hselect_eb, absorb(i.year#i.wenli) cluster(INDid HSYid) noid nofooter
gen beta16=_b[retake]
gen se16=_se[retake]
}

gen id=_n
keep if id<=16
keep beta* se* id

gen beta=0
foreach i of numlist 1 (1) 16 {
	replace beta=beta`i' if id==`i'
	gen hib`i'=beta`i'+invnormal(0.975)*se`i'
	gen lob`i'=beta`i'-invnormal(0.975)*se`i'
	gen hibw`i'=beta`i'+invnormal(0.95)*se`i'
	gen lobw`i'=beta`i'-invnormal(0.95)*se`i'
}

gen hib=0
gen lob=0
gen hibw=0
gen lobw=0
foreach i of numlist 1 (1) 16 {
	replace hib=hib`i' if id==`i'
	replace lob=lob`i' if id==`i'
	replace hibw=hibw`i' if id==`i'
	replace lobw=lobw`i' if id==`i'
}

gen ido=0
replace ido=1 if id==1 | id==5 | id==9 | id==13 
replace ido=2 if id==2 | id==6 | id==10 | id==14 
replace ido=3 if id==3 | id==7 | id==11 | id==15 
replace ido=4 if id==4 | id==8 | id==12 | id==16


gen id2=id
replace id2=id+1 if id>=5 & id<=8
replace id2=id+2 if id>=9 & id<=12
replace id2=id+3 if id>=13 & id<=16
replace id=id2

twoway (scatter beta id if ido==1, color(green) msize(large) msymbol(O)) (scatter beta id if ido==2, color(green) msize(large) msymbol(S)) (scatter beta id if ido==3, color(ebblue) msize(large)  msymbol(Oh)) (scatter beta id if ido==4, color(ebblue) msize(large) msymbol(Sh)) (rcap hib lob id if ido==1, color(green)) (rcap hib lob id if ido==2, color(green)) (rcap hib lob id if ido==3, color(ebblue)) (rcap hib lob id if ido==4, color(ebblue)) (rscatter hibw lobw id if ido==1, color(green) msymbol(X)) (rscatter hibw lobw id if ido==2, color(green) msymbol(X)) (rscatter hibw lobw id if ido==3, color(ebblue) msymbol(X)) (rscatter hibw lobw id if ido==4, color(ebblue) msymbol(X)), yline(0) legend(order(1 "Linear, Uniform Kernel" 2 "Quadratic, Uniform Kernel" 3 "Linear, Triangular Kernel" 4 "Quadratic, Triangular Kernel") row(2)) xlabel(2.5 "Bandwidth: 15" 7.5 "Bandwidth: 10" 12.5 "Bandwidth: 20" 17.5 "Bandwidth: CCT") xscale(range(0 20)) xtitle("") ytitle("Ranking") yscale(range(0)) ylabel(0 0.05 0.1 0.15 0.2) scheme(s1color) 
graph save "OutFiles\Robustness_2SLS_Pct.gph", replace
restore

gr combine "OutFiles\Robustness_2SLS_Score.gph" "OutFiles\Robustness_2SLS_Pct.gph", cols(1) ysize(10) xsize(18) scheme(s1color)
graph export "OutFiles\FigA5.png",replace

***Figure A6***
preserve
rdplot finaloutcome_eligi_t1 x_eb if abs(x_eb)<=15,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Eligible to Apply for Tier-1 University: Final") legend(off) scheme(s1color) 
graph export "OutFiles\FigA6.png",replace
restore

***Figure A7***
preserve
keep if male==1
rddensity x_eb, c(0) h(15 15) kernel(uniform) level(95) plot plot_range(-45 45) hist_range(-45 45) hist_n(9 9) genvars(v1)
twoway (bar v1_hist_height v1_hist_center if v1_hist_group==0, barwidth(5) color(green) fintensity(25)) (bar v1_hist_height v1_hist_center if v1_hist_group==1, barwidth(5) color(dkorange) fintensity(25)) (line v1_f v1_grid if v1_group==0, lcolor(green)) (line v1_f v1_grid if v1_group==1, lcolor(dkorange)) (line v1_cil v1_grid if v1_group==0, lpattern(dash) lcolor(navy)) (line v1_cir v1_grid if v1_group==0, lpattern(dash) lcolor(navy)) (line v1_cil v1_grid if v1_group==1, lpattern(dash) lcolor(navy)) (line v1_cir v1_grid if v1_group==1, lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Density") title("Male") yscale(range(0)) ylabel(0 0.001 0.002 0.003 0.004 0.005) legend(off) scheme(s1color)
graph save "OutFiles\Density_M.gph",replace
restore

preserve
keep if male==0
rddensity x_eb, c(0) h(15 15) kernel(uniform) level(95) plot plot_range(-45 45) hist_range(-45 45) hist_n(9 9) genvars(v1)
twoway (bar v1_hist_height v1_hist_center if v1_hist_group==0, barwidth(5) color(green) fintensity(25)) (bar v1_hist_height v1_hist_center if v1_hist_group==1, barwidth(5) color(dkorange) fintensity(25)) (line v1_f v1_grid if v1_group==0, lcolor(green)) (line v1_f v1_grid if v1_group==1, lcolor(dkorange)) (line v1_cil v1_grid if v1_group==0, lpattern(dash) lcolor(navy)) (line v1_cir v1_grid if v1_group==0, lpattern(dash) lcolor(navy)) (line v1_cil v1_grid if v1_group==1, lpattern(dash) lcolor(navy)) (line v1_cir v1_grid if v1_group==1, lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Density") title("Female") yscale(range(0)) ylabel(0 0.001 0.002 0.003 0.004 0.005) legend(off) scheme(s1color)
graph save "OutFiles\Density_F.gph",replace
restore

gr combine "OutFiles\Density_M.gph" "OutFiles\Density_F.gph", cols(2) ycommon ysize(10) xsize(18) scheme(s1color)
graph export "OutFiles\FigA7.png",replace

***Figure A8***
preserve
rdplot han x_eb if abs(x_eb)<=15 & male==1,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Han") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Han_M.gph",replace
restore

preserve
rdplot hui x_eb if abs(x_eb)<=15 & male==1,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Hui") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Hui_M.gph",replace
restore

preserve
rdplot urban x_eb if abs(x_eb)<=15 & male==1,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Urban") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Urban_M.gph",replace
restore

preserve
rdplot yingjie x_eb if abs(x_eb)<=15 & male==1,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("First-Time Taker") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Yingjie_M.gph",replace
restore

preserve
rdplot age x_eb if abs(x_eb)<=15 & male==1,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Age") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Age_M.gph",replace
restore

gr combine "OutFiles\Balancing_Han_M.gph" "OutFiles\Balancing_Hui_M.gph" "OutFiles\Balancing_Urban_M.gph" "OutFiles\Balancing_Yingjie_M.gph" "OutFiles\Balancing_Age_M.gph", cols(2) ysize(10) xsize(18) title("Male") scheme(s1color)
graph save "OutFiles\Balancing_M.gph",replace

preserve
rdplot han x_eb if abs(x_eb)<=15 & male==0,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Han") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Han_F.gph",replace
restore

preserve
rdplot hui x_eb if abs(x_eb)<=15 & male==0,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Hui") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Hui_F.gph",replace
restore

preserve
rdplot urban x_eb if abs(x_eb)<=15 & male==0,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Urban") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Urban_F.gph",replace
restore

preserve
rdplot yingjie x_eb if abs(x_eb)<=15 & male==0,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("First-Time Taker") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Yingjie_F.gph",replace
restore

preserve
rdplot age x_eb if abs(x_eb)<=15 & male==0,  p(1) kernel(uniform) nbins(31) ci(95) genvars hide
collapse (mean) rdplot_id rdplot_N rdplot_min_bin rdplot_max_bin rdplot_mean_bin rdplot_mean_y rdplot_se_y rdplot_ci_l rdplot_ci_r rdplot_hat_y, by(rdplot_mean_x)
twoway (scatter rdplot_mean_y rdplot_mean_x, mcolor(ebblue) msymbol(circle_hollow)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x>=0, lcolor(dkorange)) (lfit rdplot_mean_y rdplot_mean_x if rdplot_mean_x<=0, lcolor(green)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x>=0, lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x>=0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_l rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)) (line rdplot_ci_r rdplot_mean_x if rdplot_mean_x<0,  lpattern(dash) lcolor(navy)), xline(0) xtitle("Distance to Cutoff") ytitle("Age") legend(off) scheme(s1color) 
graph save "OutFiles\Balancing_Age_F.gph",replace
restore

gr combine "OutFiles\Balancing_Han_F.gph" "OutFiles\Balancing_Hui_F.gph" "OutFiles\Balancing_Urban_F.gph" "OutFiles\Balancing_Yingjie_F.gph" "OutFiles\Balancing_Age_F.gph", cols(2) ysize(10) xsize(18) title("Female") scheme(s1color)
graph save "OutFiles\Balancing_F.gph",replace

gr combine "OutFiles\Balancing_M.gph" "OutFiles\Balancing_F.gph", cols(2) ysize(10) xsize(18) scheme(s1color)
graph export "OutFiles\FigA8.png", replace

***Figure A9***
preserve

local graphvar retake

qui {
reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta1=_b[male_eb]
gen se1=_se[male_eb]
reghdfe `graphvar' male_eb below_eb male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta2=_b[male_eb]
gen se2=_se[male_eb]	
reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_15] if abs(x_eb)<=15 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta3=_b[male_eb]
gen se3=_se[male_eb]
reghdfe `graphvar' male_eb below_eb male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_15] if abs(x_eb)<=15 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta4=_b[male_eb]
gen se4=_se[male_eb]
	
reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=10 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta5=_b[male_eb]
gen se5=_se[male_eb]
reghdfe `graphvar' male_eb below_eb male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=10 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta6=_b[male_eb]
gen se6=_se[male_eb]	
reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_10] if abs(x_eb)<=10 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta7=_b[male_eb]
gen se7=_se[male_eb]
reghdfe `graphvar' male_eb below_eb male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_10] if abs(x_eb)<=10 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta8=_b[male_eb]
gen se8=_se[male_eb]

reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=20 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta9=_b[male_eb]
gen se9=_se[male_eb]
reghdfe `graphvar' male_eb below_eb male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=20 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta10=_b[male_eb]
gen se10=_se[male_eb]	
reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_20] if abs(x_eb)<=20 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta11=_b[male_eb]
gen se11=_se[male_eb]
reghdfe `graphvar' male_eb below_eb male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_20] if abs(x_eb)<=20 ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta12=_b[male_eb]
gen se12=_se[male_eb]

reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=$hselect_eb ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta13=_b[male_eb]
gen se13=_se[male_eb]
reghdfe `graphvar' male_eb below_eb male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=$hselect_eb ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta14=_b[male_eb]
gen se14=_se[male_eb]	
reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_select] if abs(x_eb)<=$hselect_eb ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta15=_b[male_eb]
gen se15=_se[male_eb]
reghdfe `graphvar' male_eb below_eb male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_select] if abs(x_eb)<=$hselect_eb ,  absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta16=_b[male_eb]
gen se16=_se[male_eb]
}

gen id=_n
keep if id<=16
keep beta* se* id

gen beta=0
foreach i of numlist 1 (1) 16 {
	replace beta=beta`i' if id==`i'
	gen hib`i'=beta`i'+invnormal(0.975)*se`i'
	gen lob`i'=beta`i'-invnormal(0.975)*se`i'
	gen hibw`i'=beta`i'+invnormal(0.95)*se`i'
	gen lobw`i'=beta`i'-invnormal(0.95)*se`i'
}

gen hib=0
gen lob=0
gen hibw=0
gen lobw=0
foreach i of numlist 1 (1) 16 {
	replace hib=hib`i' if id==`i'
	replace lob=lob`i' if id==`i'
	replace hibw=hibw`i' if id==`i'
	replace lobw=lobw`i' if id==`i'
}

gen ido=0
replace ido=1 if id==1 | id==5 | id==9 | id==13 
replace ido=2 if id==2 | id==6 | id==10 | id==14 
replace ido=3 if id==3 | id==7 | id==11 | id==15 
replace ido=4 if id==4 | id==8 | id==12 | id==16


gen id2=id
replace id2=id+1 if id>=5 & id<=8
replace id2=id+2 if id>=9 & id<=12
replace id2=id+3 if id>=13 & id<=16
replace id=id2

twoway (scatter beta id if ido==1, color(green) msize(large) msymbol(O)) (scatter beta id if ido==2, color(green) msize(large) msymbol(S)) (scatter beta id if ido==3, color(ebblue) msize(large)  msymbol(Oh)) (scatter beta id if ido==4, color(ebblue) msize(large) msymbol(Sh)) (rcap hib lob id if ido==1, color(green)) (rcap hib lob id if ido==2, color(green)) (rcap hib lob id if ido==3, color(ebblue)) (rcap hib lob id if ido==4, color(ebblue)) (rscatter hibw lobw id if ido==1, color(green) msymbol(X)) (rscatter hibw lobw id if ido==2, color(green) msymbol(X)) (rscatter hibw lobw id if ido==3, color(ebblue) msymbol(X)) (rscatter hibw lobw id if ido==4, color(ebblue) msymbol(X)), yline(0) legend(order(1 "Linear, Uniform Kernel" 2 "Quadratic, Uniform Kernel" 3 "Linear, Triangular Kernel" 4 "Quadratic, Triangular Kernel") row(2)) xlabel(2.5 "Bandwidth: 15" 7.5 "Bandwidth: 10" 12.5 "Bandwidth: 20" 17.5 "Bandwidth: CCT") xscale(range(0 20)) xtitle("") ytitle("") yscale(range(0)) scheme(s1color) ysize(10) xsize(18)
graph export "OutFiles\FigA9.png", replace
restore

***Figure A10***
preserve

local graphvar retake

qui {
reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & age<=17, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta1=_b[male_eb]
gen se1=_se[male_eb]

reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & age==18, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta2=_b[male_eb]
gen se2=_se[male_eb]

reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & age==19, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta3=_b[male_eb]
gen se3=_se[male_eb]

reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & age==20, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta4=_b[male_eb]
gen se4=_se[male_eb]

reghdfe `graphvar' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & age>=21, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
gen beta5=_b[male_eb]
gen se5=_se[male_eb]

}

gen id=_n
keep if id<=5
keep beta* se* id

gen beta=0
foreach i of numlist 1 (1) 5 {
	replace beta=beta`i' if id==`i'
	gen hib`i'=beta`i'+invnormal(0.975)*se`i'
	gen lob`i'=beta`i'-invnormal(0.975)*se`i'
	gen hibw`i'=beta`i'+invnormal(0.95)*se`i'
	gen lobw`i'=beta`i'-invnormal(0.95)*se`i'
}

gen hib=0
gen lob=0
gen hibw=0
gen lobw=0
foreach i of numlist 1 (1) 5 {
	replace hib=hib`i' if id==`i'
	replace lob=lob`i' if id==`i'
	replace hibw=hibw`i' if id==`i'
	replace lobw=lobw`i' if id==`i'
}

gen id2=id
replace id2=id+1 if id==2
replace id2=id+2 if id==3
replace id2=id+3 if id==4
replace id2=id+4 if id==5
replace id=id2

twoway (scatter beta id , color(green) msize(large) msymbol(O)) (rcap hib lob id, color(green)) (rscatter hibw lobw id, color(green) msymbol(X)) , yline(0) legend(off) xlabel(1 "<=17" 3 "18" 5 "19" 7 "20" 9 ">=21") xscale(range(0 10)) xtitle("Age") ytitle("") yscale(range(0)) scheme(s1color) ysize(10) xsize(18)
graph export "OutFiles\FigA10.png", replace
restore

***Figure A11***
preserve

local graphvar dif_std_score

qui {
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta1=_b[male_retake]
gen se1=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta2=_b[male_retake]
gen se2=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_15] if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta3=_b[male_retake]
gen se3=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_15] if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta4=_b[male_retake]
gen se4=_se[male_retake]

ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=10 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta5=_b[male_retake]
gen se5=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=10 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta6=_b[male_retake]
gen se6=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_10] if abs(x_eb)<=10 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta7=_b[male_retake]
gen se7=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_10] if abs(x_eb)<=10 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta8=_b[male_retake]
gen se8=_se[male_retake]

ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=20 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta9=_b[male_retake]
gen se9=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=20 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta10=_b[male_retake]
gen se10=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_20] if abs(x_eb)<=20 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta11=_b[male_retake]
gen se11=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_20] if abs(x_eb)<=20 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta12=_b[male_retake]
gen se12=_se[male_retake]

ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta13=_b[male_retake]
gen se13=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta14=_b[male_retake]
gen se14=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_select] if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta15=_b[male_retake]
gen se15=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_select] if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta16=_b[male_retake]
gen se16=_se[male_retake]
}

gen id=_n
keep if id<=16
keep beta* se* id

gen beta=0
foreach i of numlist 1 (1) 16 {
	replace beta=beta`i' if id==`i'
	gen hib`i'=beta`i'+invnormal(0.975)*se`i'
	gen lob`i'=beta`i'-invnormal(0.975)*se`i'
	gen hibw`i'=beta`i'+invnormal(0.95)*se`i'
	gen lobw`i'=beta`i'-invnormal(0.95)*se`i'
}

gen hib=0
gen lob=0
gen hibw=0
gen lobw=0
foreach i of numlist 1 (1) 16 {
	replace hib=hib`i' if id==`i'
	replace lob=lob`i' if id==`i'
	replace hibw=hibw`i' if id==`i'
	replace lobw=lobw`i' if id==`i'
}

gen ido=0
replace ido=1 if id==1 | id==5 | id==9 | id==13 
replace ido=2 if id==2 | id==6 | id==10 | id==14 
replace ido=3 if id==3 | id==7 | id==11 | id==15 
replace ido=4 if id==4 | id==8 | id==12 | id==16


gen id2=id
replace id2=id+1 if id>=5 & id<=8
replace id2=id+2 if id>=9 & id<=12
replace id2=id+3 if id>=13 & id<=16
replace id=id2

twoway (scatter beta id if ido==1, color(green) msize(large) msymbol(O)) (scatter beta id if ido==2, color(green) msize(large) msymbol(S)) (scatter beta id if ido==3, color(ebblue) msize(large)  msymbol(Oh)) (scatter beta id if ido==4, color(ebblue) msize(large) msymbol(Sh)) (rcap hib lob id if ido==1, color(green)) (rcap hib lob id if ido==2, color(green)) (rcap hib lob id if ido==3, color(ebblue)) (rcap hib lob id if ido==4, color(ebblue)) (rscatter hibw lobw id if ido==1, color(green) msymbol(X)) (rscatter hibw lobw id if ido==2, color(green) msymbol(X)) (rscatter hibw lobw id if ido==3, color(ebblue) msymbol(X)) (rscatter hibw lobw id if ido==4, color(ebblue) msymbol(X)), yline(0) legend(order(1 "Linear, Uniform Kernel" 2 "Quadratic, Uniform Kernel" 3 "Linear, Triangular Kernel" 4 "Quadratic, Triangular Kernel") row(2)) xlabel(2.5 "Bandwidth: 15" 7.5 "Bandwidth: 10" 12.5 "Bandwidth: 20" 17.5 "Bandwidth: CCT") xscale(range(0 20)) xtitle("") ytitle("Standardized Score") yscale(range(0)) scheme(s1color) 
graph save "OutFiles\Robustness_2SLSDif_Score.gph", replace
restore


preserve

local graphvar dif_pct

qui {
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta1=_b[male_retake]
gen se1=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta2=_b[male_retake]
gen se2=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_15] if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta3=_b[male_retake]
gen se3=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_15] if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta4=_b[male_retake]
gen se4=_se[male_retake]

ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=10 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta5=_b[male_retake]
gen se5=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=10 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta6=_b[male_retake]
gen se6=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_10] if abs(x_eb)<=10 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta7=_b[male_retake]
gen se7=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_10] if abs(x_eb)<=10 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta8=_b[male_retake]
gen se8=_se[male_retake]

ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=20 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta9=_b[male_retake]
gen se9=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=20 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta10=_b[male_retake]
gen se10=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_20] if abs(x_eb)<=20 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta11=_b[male_retake]
gen se11=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_20] if abs(x_eb)<=20 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta12=_b[male_retake]
gen se12=_se[male_retake]

ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta13=_b[male_retake]
gen se13=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta14=_b[male_retake]
gen se14=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_select] if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta15=_b[male_retake]
gen se15=_se[male_retake]
ivreghdfe `graphvar' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male [w=K_eb_select] if abs(x_eb)<=$hselect_eb , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) noid nofooter
gen beta16=_b[male_retake]
gen se16=_se[male_retake]
}

gen id=_n
keep if id<=16
keep beta* se* id

gen beta=0
foreach i of numlist 1 (1) 16 {
	replace beta=beta`i' if id==`i'
	gen hib`i'=beta`i'+invnormal(0.975)*se`i'
	gen lob`i'=beta`i'-invnormal(0.975)*se`i'
	gen hibw`i'=beta`i'+invnormal(0.95)*se`i'
	gen lobw`i'=beta`i'-invnormal(0.95)*se`i'
}

gen hib=0
gen lob=0
gen hibw=0
gen lobw=0
foreach i of numlist 1 (1) 16 {
	replace hib=hib`i' if id==`i'
	replace lob=lob`i' if id==`i'
	replace hibw=hibw`i' if id==`i'
	replace lobw=lobw`i' if id==`i'
}

gen ido=0
replace ido=1 if id==1 | id==5 | id==9 | id==13 
replace ido=2 if id==2 | id==6 | id==10 | id==14 
replace ido=3 if id==3 | id==7 | id==11 | id==15 
replace ido=4 if id==4 | id==8 | id==12 | id==16


gen id2=id
replace id2=id+1 if id>=5 & id<=8
replace id2=id+2 if id>=9 & id<=12
replace id2=id+3 if id>=13 & id<=16
replace id=id2

twoway (scatter beta id if ido==1, color(green) msize(large) msymbol(O)) (scatter beta id if ido==2, color(green) msize(large) msymbol(S)) (scatter beta id if ido==3, color(ebblue) msize(large)  msymbol(Oh)) (scatter beta id if ido==4, color(ebblue) msize(large) msymbol(Sh)) (rcap hib lob id if ido==1, color(green)) (rcap hib lob id if ido==2, color(green)) (rcap hib lob id if ido==3, color(ebblue)) (rcap hib lob id if ido==4, color(ebblue)) (rscatter hibw lobw id if ido==1, color(green) msymbol(X)) (rscatter hibw lobw id if ido==2, color(green) msymbol(X)) (rscatter hibw lobw id if ido==3, color(ebblue) msymbol(X)) (rscatter hibw lobw id if ido==4, color(ebblue) msymbol(X)), yline(0) legend(order(1 "Linear, Uniform Kernel" 2 "Quadratic, Uniform Kernel" 3 "Linear, Triangular Kernel" 4 "Quadratic, Triangular Kernel") row(2)) xlabel(2.5 "Bandwidth: 15" 7.5 "Bandwidth: 10" 12.5 "Bandwidth: 20" 17.5 "Bandwidth: CCT") xscale(range(0 20)) xtitle("") ytitle("Ranking") yscale(range(0)) scheme(s1color) 
graph save "OutFiles\Robustness_2SLSDif_Pct.gph", replace
restore

gr combine "OutFiles\Robustness_2SLSDif_Score.gph" "OutFiles\Robustness_2SLSDif_Pct.gph", cols(1) ysize(10) xsize(18) scheme(s1color)
graph export "OutFiles\FigA11.png",replace


***Table 1***
local vars_summary male han hui urban yingjie age retake
eststo col1:estpost sum `vars_summary' 
eststo col2:estpost sum `vars_summary' if abs(x_eb)<=15
eststo col3:estpost sum `vars_summary' if abs(x_eb)<=15 & x_eb<0
eststo col4:estpost sum `vars_summary' if abs(x_eb)<=15 & x_eb>=0
esttab col1 col2 col3 col4 using "OutFiles\Tab1.rtf", replace cells(mean(fmt(2)) sd(par fmt(2)))  nomtitle title(Summary Statistics)
clear matrix

***Table 2***
local BaseControl male han hui urban yingjie age

foreach v of varlist retake {
	reghdfe `v' below_eb x_eb int_below_eb if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, No, Cluster, Yes)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, No, Cluster, Yes)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(below_eb) 
}

***Table 3***
local BaseControl male han hui urban yingjie age

foreach v of varlist currentoutcome_std_score currentoutcome_pct finaloutcome_std_score finaloutcome_pct dif_std_score dif_pct {
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab3.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab3.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(below_eb) 
}

foreach v of varlist dif_std_score dif_pct {
	ivreghdfe `v' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) 
	outreg2 using "OutFiles\Tab3.xls",stats(coef se) bdec(4) sdec(4) addstat(1st-stage KP F-stat, e(widstat)) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(retake) 
	ivreghdfe `v' (retake=below_eb) x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid) 
	outreg2 using "OutFiles\Tab3.xls",stats(coef se) bdec(4) sdec(4) addstat(1st-stage KP F-stat, e(widstat)) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(retake) 
}

***Table 4***
local BaseControl male han hui urban yingjie age

foreach v of varlist retake {
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15 & male==1, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab4.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Male, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15 & male==0, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab4.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Female, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab4.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, With Gender Interaction, Cluster, Yes)  keep(male_eb below_eb)
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han hui urban yingjie age if abs(x_eb)<=15 , absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab4.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Without Gender Interaction, Cluster, Yes)  keep(male_eb below_eb male) 

}

foreach v of varlist retake {
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15 & male==1, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab4.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Male, Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15 & male==0, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab4.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Female, Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab4.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, With Gender Interaction, Cluster, Yes)  keep(male_eb below_eb)
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han hui urban yingjie age if abs(x_eb)<=15 , absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab4.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Without Gender Interaction, Cluster, Yes)  keep(male_eb below_eb male) 
}

***Tables 5-6***
foreach v of varlist retake {
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & han==1, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Han, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & han==0, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Minority, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & urban==1, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Urban, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & urban==0, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Rural, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & wenli==1, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Art, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & wenli==2, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Science, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & yingjie==1, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, First-Time Taker, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & yingjie==0, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Repeated Taker, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
}


foreach v of varlist retake {
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & hs_highq_bytrack==1, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, High Quality HS, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & hs_highq_bytrack==0, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Low Quality HS, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & hs_highsexr_nobytrack==1, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, High Sex Ratio HS, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & hs_highsexr_nobytrack==0, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Low Sex Ratio HS, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb)
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & ct_highsexr==1, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, High Sex Ratio County, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & ct_highsexr==0, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Low Sex Ratio County, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb)
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & ct_highgdp==1, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, High GDP County, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 & ct_highgdp==0, absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\Tab6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Low GDP County, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
}

*MHT*
*mhtreg (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & han==1) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & han==0) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & urban==1) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & urban==0) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & wenli==1) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & wenli==2) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & yingjie==1) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & yingjie==0) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & hs_highq_bytrack==1) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & hs_highq_bytrack==0) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & hs_highsexr_nobytrack==1) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & hs_highsexr_nobytrack==0) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & ct_highsexr==1) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & ct_highsexr==0) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & ct_highgdp==1) (retake male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male i.year#i.wenli#i.male if abs(x_eb)<=15 & ct_highgdp==0), cluster(HSYid) cltype(3) seed(10086) bootstrap(2000)

***Table 8***
local BaseControl male han hui urban yingjie age

foreach v of varlist dif_std_score dif_pct  {
	ivreghdfe `v' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=15 & male==1, absorb(i.year#i.wenli) cluster(INDid HSYid) 
	outreg2 using "OutFiles\Tab8.xls",stats(coef se) bdec(4) sdec(4) addstat(1st-stage KP F-stat, e(widstat)) ctitle(`v') addtext(Sample, Male, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_retake retake) 
	ivreghdfe `v' (retake=below_eb) x_eb int_below_eb `BaseControl' if abs(x_eb)<=15 & male==0, absorb(i.year#i.wenli) cluster(INDid HSYid) 
	outreg2 using "OutFiles\Tab8.xls",stats(coef se) bdec(4) sdec(4) addstat(1st-stage KP F-stat, e(widstat)) ctitle(`v') addtext(Sample, Female, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_retake retake) 
	ivreghdfe `v' (male_retake retake= male_eb below_eb) male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid) 
	outreg2 using "OutFiles\Tab8.xls",stats(coef se) bdec(4) sdec(4) addstat(1st-stage KP F-stat, e(widstat)) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_retake retake) 
}

***Table A1***
foreach v of varlist male han hui urban yingjie age {
	reghdfe `v' below_eb x_eb int_below_eb if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA1.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Cluster, Yes)  keep(below_eb) 
}

foreach v of varlist male han hui urban yingjie age {
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA1.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Cluster, Yes)  keep(below_eb) 
}

***Table A2***
local BaseControl male han hui urban yingjie age

foreach v of varlist retake {
	reghdfe `v' below_eb x_eb int_below_eb if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid hsid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, No, Cluster, B)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid hsid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, B)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid hsid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, No, Cluster, B)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid hsid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, B)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(cl_x_eb INDid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, No, Cluster, C)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(cl_x_eb INDid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, C)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(cl_x_eb INDid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, No, Cluster, C)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(cl_x_eb INDid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, C)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(cl_x_eb HSYid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, No, Cluster, D)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(cl_x_eb HSYid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, D)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(cl_x_eb HSYid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, No, Cluster, D)  keep(below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(cl_x_eb HSYid)
	outreg2 using "OutFiles\TabA2.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, D)  keep(below_eb) 
}

***Table A3***
local vars_summary currentoutcome_std_score currentoutcome_pct finaloutcome_std_score finaloutcome_pct 
eststo col1:estpost sum `vars_summary' 
eststo col2:estpost sum `vars_summary' if abs(x_eb)<=15
eststo col3:estpost sum `vars_summary' if abs(x_eb)<=15 & x_eb<0
eststo col4:estpost sum `vars_summary' if abs(x_eb)<=15 & x_eb>=0
esttab col1 col2 col3 col4 using "OutFiles\TabA3.rtf", replace cells(mean(fmt(2)) sd(par fmt(2)))  nomtitle title(Summary Statistics)
clear matrix

***Table A4***
reghdfe retake male, absorb(i.year#i.wenli) cluster(INDid HSYid)
outreg2 using "OutFiles\TabA4.xls",stats(coef se) bdec(4) sdec(4) ctitle(retake) addtext(Sample, Full)  keep(male han hui urban yingjie age std_score) 
reghdfe retake male han hui urban yingjie age std_score , absorb(i.year#i.wenli) cluster(INDid HSYid)
outreg2 using "OutFiles\TabA4.xls",stats(coef se) bdec(4) sdec(4) ctitle(retake) addtext(Sample, Full)  keep(male han hui urban yingjie age std_score) 
reghdfe retake male if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
outreg2 using "OutFiles\TabA4.xls",stats(coef se) bdec(4) sdec(4) ctitle(retake) addtext(Sample, RD)  keep(male han hui urban yingjie age std_score) 
reghdfe retake male han hui urban yingjie age std_score if abs(x_eb)<=15, absorb(i.year#i.wenli) cluster(INDid HSYid)
outreg2 using "OutFiles\TabA4.xls",stats(coef se) bdec(4) sdec(4) ctitle(retake) addtext(Sample, RD)  keep(male han hui urban yingjie age std_score) 

***Table A5***
foreach v of varlist han hui urban yingjie age {
	reghdfe `v' below_eb x_eb int_below_eb if abs(x_eb)<=15 & male==1, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Sample, M, Cluster, Yes)  keep(below_eb) 
}
foreach v of varlist han hui urban yingjie age {
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 if abs(x_eb)<=15 & male==1, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Sample, M, Cluster, Yes)  keep(below_eb) 
}

foreach v of varlist han hui urban yingjie age {
	reghdfe `v' below_eb x_eb int_below_eb if abs(x_eb)<=15 & male==0, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Sample, F, Cluster, Yes)  keep(below_eb) 
}
foreach v of varlist han hui urban yingjie age {
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 if abs(x_eb)<=15 & male==0, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA5.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Sample, F, Cluster, Yes)  keep(below_eb) 
}

***Table A6***
local BaseControl male han hui urban yingjie age

foreach v of varlist retake {
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15 & yingjie==1 & male==1, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Male, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb `BaseControl' if abs(x_eb)<=15 & yingjie==1 & male==0, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Female, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if yingjie==1 & abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, With Gender Interaction, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male han hui urban yingjie age if yingjie==1 & abs(x_eb)<=15 , absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Linear, Kernel, Uniform, Year-Track FE, Yes, Controls, Without Gender Interaction, Cluster, Yes)  keep(male_eb below_eb male) 
}

foreach v of varlist retake {
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15 & yingjie==1 & male==1, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Male, Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' below_eb x_eb int_below_eb x_eb2 int_below_eb2 `BaseControl' if abs(x_eb)<=15 & yingjie==1 & male==0, absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Female, Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Yes, Cluster, Yes)  keep(male_eb below_eb) 
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han##i.male hui##i.male urban##i.male yingjie##i.male c.age##i.male if yingjie==1 & abs(x_eb)<=15 , absorb(i.year#i.wenli#i.male) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, With Gender Interaction, Cluster, Yes)  keep(male_eb below_eb)
	reghdfe `v' male_eb below_eb male c.x_eb##i.male c.int_below_eb##i.male c.x_eb2##i.male c.int_below_eb2##i.male han hui urban yingjie age if yingjie==1 & abs(x_eb)<=15 , absorb(i.year#i.wenli) cluster(INDid HSYid)
	outreg2 using "OutFiles\TabA6.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') addtext(Sample, Full, Bandwidth, 15, Interaction Controls, Quadratic, Kernel, Uniform, Year-Track FE, Yes, Controls, Without Gender Interaction, Cluster, Yes)  keep(male_eb below_eb male) 
}





clear
use "Data\Survey_Analysis.dta",clear
***Table A7***
local outcome repeat_avg repeat_yiben repeat_target
local characteristics feduc_2 feduc_3 feduc_4 feduc_5 meduc_2 meduc_3 meduc_4 meduc_5 faminc_2 faminc_3 faminc_4 numsib_2 numsib_3 numsib_4 minority
local mechanism risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ

local vars_baseline `outcome' `characteristics' `mechanism'
eststo col1:estpost sum `vars_baseline' if male==0
eststo col2:estpost sum `vars_baseline' if male==1
eststo col3:estpost ttest `vars_baseline' , by(male)
estadd matrix mean=e(b)
estadd matrix sd=e(se)
esttab col1 col2 col3 using "OutFiles\TabA7.rtf", replace cells(mean(fmt(3)) sd(par)) title(Summary Statistics, by Gender) mtitles ("Female" "Male" "Difference") 

***Tables A8-A9***
local risk risktaking 
local return return_yiben_avg return_erben_avg return_yiben_self return_erben_self
local cost cost_marriage cost_psych 
local support fam_support fam_exp 
local confidence confidence_retake confidence_normaltake
local compete compete_willingness
local attri attri_recent attri_ncee
local locus locus_in locus_educ
local famcha feduc_2 feduc_3 feduc_4 feduc_5 meduc_2 meduc_3 meduc_4 meduc_5 faminc_2 faminc_3 faminc_4 numsib_2 numsib_3 numsib_4 minority

gen mechnomiss=(risktaking!=. & return_yiben_avg!=. & return_erben_avg!=. & return_yiben_self!=. & return_erben_self!=. & cost_marriage!=. & cost_psych!=. & fam_support!=. & fam_exp!=. & confidence_retake!=. & confidence_normaltake!=. & compete_willingness!=. & attri_recent!=. & attri_ncee!=. & locus_in!=. & locus_educ!=.)

foreach v of varlist repeat_avg {
	reg `v' male, robust
	outreg2 using "OutFiles\TabA8.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') keep(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ) sortvar(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ)
	reg `v' male i.schoolid#i.grade#i.stem, robust
	outreg2 using "OutFiles\TabA8.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') keep(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ) sortvar(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ)
	reg `v' male `famcha' i.schoolid#i.grade#i.stem, robust
	outreg2 using "OutFiles\TabA8.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') keep(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ) sortvar(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ)
	reg `v' male `famcha' i.schoolid#i.grade#i.stem if mechnomiss==1, robust
	outreg2 using "OutFiles\TabA8.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') keep(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ) sortvar(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ)
	reg `v' male `famcha' `risk' `return' `cost' `support' `confidence' `compete' `attri' `locus' i.schoolid#i.grade#i.stem if mechnomiss==1, robust
	outreg2 using "OutFiles\TabA8.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') keep(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ) sortvar(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ)
}

foreach v of varlist repeat_yiben repeat_target {
	reg `v' male, robust
	outreg2 using "OutFiles\TabA9.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') keep(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ) sortvar(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ)
	reg `v' male `famcha' i.schoolid#i.grade#i.stem if mechnomiss==1, robust
	outreg2 using "OutFiles\TabA9.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') keep(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ) sortvar(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ)
	reg `v' male `famcha' `risk' `return' `cost' `support' `confidence' `compete' `attri' `locus' i.schoolid#i.grade#i.stem if mechnomiss==1, robust
	outreg2 using "OutFiles\TabA9.xls",stats(coef se) bdec(4) sdec(4) ctitle(`v') keep(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ) sortvar(male risktaking return_yiben_avg return_erben_avg return_yiben_self return_erben_self cost_marriage cost_psych fam_support fam_exp confidence_retake confidence_normaltake compete_willingness attri_recent attri_ncee locus_in locus_educ)
}

***Table 9***
egen schoolgradestem=group(schoolid grade stem)
foreach i of numlist 2 (1) 10 {
	gen sgs_`i'=(schoolgradestem==`i')
}

local SGS sgs_2 sgs_3 sgs_4 sgs_5 sgs_6 sgs_7 sgs_8 sgs_9 sgs_10

local risk risktaking 
local return return_yiben_avg return_erben_avg return_yiben_self return_erben_self
local cost cost_marriage cost_psych 
local support fam_support fam_exp 
local confidence confidence_retake confidence_normaltake
local compete compete_willingness
local attri attri_recent attri_ncee
local locus locus_in locus_educ
local famcha feduc_2 feduc_3 feduc_4 feduc_5 meduc_2 meduc_3 meduc_4 meduc_5 faminc_2 faminc_3 faminc_4 numsib_2 numsib_3 numsib_4 minority

oaxaca repeat_avg `famcha' `risk' `return' `cost' `support' `confidence' `compete' `attri' `locus' `SGS' if mechnomiss==1, by (male) pooled detail(family: `famcha', risk:`risk', return:`return', cost:`cost', support: `support', confidence: `confidence', compete: `compete', attribution:`attri', locus:`locus', SGS:`SGS') vce(robust)

*****end*****
